Spring Boot and Thymeleaf 常量与文字处理(Literals and Text operations)
Literals
在Thymeleaf中,我们将Literals翻译为类型,即Thymeleaf中和其他语言一样,共包含了字符串、数字、 布尔、操作符等四种数据类型,本章将会对这四种数据类型做出实例及相关解释和实现。
字符串
字符串即你输入的任何字母都叫字符串类型,英文即 String,代表着字符串类型,按道理讲常见的各种字母和数字都可被字符串所海纳。
1 2 3 4 5 6 7 8 9 10 11 12 13
| <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> <link th:href="@{/css/style.css}" href="../static/css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <h5>Java Thymeleaf String</h5> <hr /> <p>Hello,world<span th:text="'The on html+thymeleaf file'">The one html file</span></p> </body> </html>
|
之所以上图输出的文字是红色的,也许可能是因为 /css/style.css的原因,读者可自行进行编写,如你可以编写一个 :
p {color: green}
让生活多一点乐趣与对生活更或者世界的向往,看开一切,展开心扉,无所畏惧,你将变得更加美好。
String + var
Thymeleaf还支持数据类型与变量格式化的内容,即在一个""下输出信息,这个支持方便了很多,支持的也很多,语法为:
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13
| <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> <link th:href="@{/css/style.css}" href="../static/css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <h5>Java Thymeleaf Literals</h5> <hr /> <p th:text="|Welcome to Thymeleaf, ${data}!|"></p> </body> </html>
|
urlController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.example.demo;
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping;
@Controller public class urlController { @GetMapping("index") public String getindex(Model model) { model.addAttribute("data","Thymeleaf"); return "index"; } }
|
数字
数字(int),即0~9个数字组合的一种数据,这种你可以随便组合比如7758258这些,当然我也不用太过多的介绍,我相信读者们都有一定的数据类型基础,毕竟学各种语言的时候都会学习到数据类型,本次我们将会使用Thylemeaf来实现一个数字类型:
1 2 3 4 5 6 7 8 9 10 11 12 13
| <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> <link th:href="@{/css/style.css}" href="../static/css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <h5>Java Thymeleaf String</h5> <hr /> <p>Hello,world <span th:text="2020">The one html file</span></p> </body> </html>
|
算数运算符
在众多语言之中,算数运算符和数据类型一样,都是一个必经之路,所以本文不做过多演示,刚好今天是2021年1月6日,晚 01:18:07 点,就利用算数运算符祝大家2021年新年快乐吧:
1 2 3 4 5 6 7 8 9 10 11 12 13
| <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> <link th:href="@{/css/style.css}" href="../static/css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <h5>Java Thymeleaf String</h5> <hr /> <p>Hello,world <span th:text="2020 + 1">The one html file</span></p> </body> </html>
|
String + 算数运算符
1 2 3 4 5 6 7 8 9 10 11 12 13
| <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> <link th:href="@{/css/style.css}" href="../static/css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <h5>Java Thymeleaf Literals</h5> <hr /> <p th:text="'Hello,' + ${data} + '!'">The one html file</p> </body> </html>
|
在Thymeleaf模板引擎之中,我们可以通过数据类型和算数运算符进行叠加(虽然用的是字符串类型),通过算数运算符将数据类型与变量相加将会得到组合后的结果。
布尔
布尔也是开发语言三件套之一,通常布尔用于判断和各种循环条件之中,本书将会使用Thymeleaf模板引擎来实现布尔的一系列操作:
在Thymeleaf模板引擎之中,布尔的写法是有区别的如**<div th:if=“${data} == ‘thymeleaf’”<div th:if=”${data=’Thymeleaf’”**,两种写法其中第一种写法即布尔在大阔号外的即将会由Thymeleaf负责处理,如果将布尔写在大阔号内将会由OGNL/SpringEL引擎负责处理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> <link th:href="@{/css/style.css}" href="../static/css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <h5>Java Thymeleaf boolean</h5> <hr /> <div th:if="${data}=='Thymeleaf'"> <p>Thymeleaf....if $data 变量正确</p> </div> </body> </html>
|
if and unless
本书通过if and unless来实现判断data变量是否为空作为案例,如data变量为空则输出不为空等信息,实现方法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> <link th:href="@{/css/style.css}" href="../static/css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <h5>Java Thymeleaf boolean</h5> <hr /> <div th:if="${data} == true"> <p>The true urlController $data</p> </div> <div th:unless="${data} == true"> <p>The false urlController $data</p> </div>
</body> </html>
|
在Thymeleaf之中,th:if与th:unless的意思和功能是不同的,通常为th:is是条件成立时执行的内容而th:unless则是条件不成立时执行的内容
Text operations
算数运算符
算数运算符之中,主要包含和使用+、-、*、/、%等算数符号,在此的过程之中,将会由OGNL引擎来代替Thymeleaf标准表达式引擎执行,显然这也是另一个写法,通常算数运算符包含在大阔号中的将会由OGNL引擎代替,而写在大阔号外的则由Thymeleaf引擎执 ,算数运算符和其他开发语言中的相差无几,本书将不详述,有兴趣的读者可自行了解
比较器
在Thymeleaf之中,比较器和上述的算数运算符不同,比较器的写法可能与其他开发语言不同,如:
| ID |
DA |
| > |
gt |
| < |
it |
| >= |
ge |
| <= |
le |
| ! |
not |
| == |
eq |
| != |
neq/ne |
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> <link th:href="@{/css/style.css}" href="../static/css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <h5>Java Thymeleaf comparator</h5> <hr /> <div th:if="${data} eq 'Thymeleaf'"> <p>urlController comparator eq(==) Thymeleaf</p> </div> </body> </html>
|
默认表达式
在Thymeleaf之中,与其说是默认表达式不如说是 if……else的在称,其意思为,如当前指定的变量与条件不符则默认输出指定信息或变量,这个很类似于else :
urlController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.example.demo;
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping;
@Controller public class urlController { @GetMapping("index") public String getindex(Model model) { data dataone = new data(null ,20); model.addAttribute("data",dataone); return "index"; } }
|
data.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.example.demo;
public class data { private String name; private int age;
public data(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
|
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> <link th:href="@{/css/style.css}" href="../static/css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <h5>Java Thymeleaf Elvis</h5> <hr /> <div th:object="${data}"> <p>Name: <span th:text="*{name}?: '(No name? Are you ok?)'"></span></p> </div> </body> </html>
|
原型文本默认表达式
原型文本默认表达式在Thymeleaf官方文档中的名称叫做(The No-Operation token)即“禁止操作令牌”,其主要由令牌下划线符号"_"来表示,这样看起来代码简洁而美丽且大方(虽然IDE开发环境中会报错,但依然好看):
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> <link th:href="@{/css/style.css}" href="../static/css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <h5>Java Thymeleaf Elvis</h5> <hr /> <div th:object="${data}"> <p>Name: <span th:text="*{name} ?: _">No name?Are you ok?</span></p> </div> </body> </html>
|
这样的写法即约等于了 <p>Name: <span th:text="*{name}?: '(No name? Are you ok?)'"></span></p>即默认表达式,使用原型文本默认表达式不仅代码美观且大气,还显得格外诱人。
数据转换与格式化
Thymeleaf提供数据转换与格式化的表达式,通过数据的转换来让其变成字符串类型(String),无论变量数据是什么格式,都将被转化为字符串类型,本书例子我们会将数字类型转换为字符串类型:
inde.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> <link th:href="@{/css/style.css}" href="../static/css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <h5>Java Thymeleaf The No-Operation token</h5> <hr /> <td th:text="${{data.age}}"></td> </div> </body> </html>
|
原本的data.age本是数字类型,但通过两个大阔号即{{……}}可让其自动转换/格式化为字符串类型。
⬅️ Go back